home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12296 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  43 lines

  1. Path: news.onramp.net!usenet
  2. From: Lee Meador <rtbs@onramp.net>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: string search?
  5. Date: Wed, 27 Mar 1996 10:02:30 -0800
  6. Organization: RealTime
  7. Message-ID: <315982B6.2113@onramp.net>
  8. References: <4jc6ae$eol@voyager.eng.gulfaero.com> <4jdot7$eb0@sparcserver.lrz-muenchen.de> <828126918snz@genesis.demon.co.uk>
  9. NNTP-Posting-Host: stemmons31.onramp.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win16; I)
  14. CC: Ricardo Mor <rmor1@ix.netcom.com>
  15.  
  16. A bunch of people wrote how:
  17. > >>Ricardo Mor  <rmor1@ix.netcom.com> wrote:
  18. > >>>    How can I search for a string of characters within another string?
  19.  
  20. char *p;
  21.  
  22.     str1 = "TIME";
  23.     str2 = "NEXT_TIME";
  24.  
  25.     for (p = str2; (*p); ++p) {
  26.         if (*p == *str1 && strcmp(p, str1) == 0) {
  27.             /* It matches */
  28.             break;
  29.             }
  30.         }
  31.     if (*p) {
  32.         /* It doesn't match */
  33.         }
  34.  
  35. NOTE: You could put in an inner loop and write your own 'strcmp' if you like 
  36. to avoid function calls altogether.
  37.  
  38. NOTE2: This is all best put into a function that we could call something 
  39. clever. Say, we could call it 'strstr'. It would take str1 and str2 as 
  40. parameters and return a pointer to the matching point or NULL if no match.
  41.  
  42. -- Lee Meador
  43.